//
//  ResetPasswordViewController.m
//  KIASupervisory
//
//  Created by tan_xin on 14-6-30.
//  Copyright (c) 2014年 MyCompany. All rights reserved.
//

#import "ResetPasswordViewController.h"
#import "FindPasswordViewController.h"
#import "LoginViewController.h"


@interface ResetPasswordViewController ()
@property (weak, nonatomic) IBOutlet UITextField *pwdTxt;
@property (weak, nonatomic) IBOutlet UITextField *repwdTxt;
@end

@implementation ResetPasswordViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillAppear:(BOOL)animated {
    
    self.showLeftBtn = YES;
    self.showRightBtn = NO;
    self.title = @"设置密码";
    
    [super viewWillAppear:animated];
}

- (void)leftNavItemAction:(id)sender {
    AppDelegate *dele = [AppDelegate sharedDelegate];
    DDMenuController *menuVC = dele.ddLeftMenu;
    [menuVC showLeft:nil];
}

- (IBAction)backToPre:(id)sender {
    FindPasswordViewController *findVC = (FindPasswordViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"FindPassword"];
//    [self.navigationController pushViewController:findVC animated:YES];
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = kCATransitionPush; //kCATransitionMoveIn; //, kCATransitionPush, kCATransitionReveal, kCATransitionFade
    transition.subtype = kCATransitionFromLeft; //kCATransitionFromLeft, kCATransitionFromRight, kCATransitionFromTop, kCATransitionFromBottom
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController pushViewController:findVC animated:NO];
}

// 密码验证
- (IBAction)checkPwd:(id)sender {
    if (![self isBlankString:self.pwdTxt.text]) {
        NSString *regex =[NSString stringWithFormat:@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$"];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        BOOL isMatch = [predicate evaluateWithObject:self.pwdTxt.text];
        
        if(!isMatch){
            NSString *regex2 =[NSString stringWithFormat:@"^[0-9A-Za-z]{4,20}$"];
            NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
            BOOL isMatch2 = [predicate2 evaluateWithObject:self.pwdTxt.text];
            NSString *msm = @"输入不正确，密码必须包含字母和数字且4-20位";
            if(isMatch2){
                msm = @"密码不能全为数字或字母";
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:msm
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return;
        } else if(![self isBlankString:self.repwdTxt.text] && ![self.repwdTxt.text isEqualToString:(self.pwdTxt.text)]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"两次密码不一致"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return;
        }
    }
    
}

//确认密码验证
- (IBAction)checkRePwd:(id)sender {
    NSLog(@"@@@@@@@@%d",![self isBlankString:self.pwdTxt.text]);
    NSLog(@"@@@@@@@@%d",![self isBlankString:self.repwdTxt.text]);
    if (![self isBlankString:self.repwdTxt.text]) {
        NSString *regex =[NSString stringWithFormat:@"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{4,20}$"];
        NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
        BOOL isMatch = [predicate evaluateWithObject:self.repwdTxt.text];
        
        if(!isMatch){
            NSString *regex2 =[NSString stringWithFormat:@"^[0-9A-Za-z]{4,20}$"];
            NSPredicate *predicate2 = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex2];
            BOOL isMatch2 = [predicate2 evaluateWithObject:self.repwdTxt.text];
            
            NSString *msm = @"输入不正确，密码必须包含字母和数字且4-20位";
            if(isMatch2){
                msm = @"密码不能全为数字或字母";
            }
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:msm
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return;
        } else if(![self isBlankString:self.pwdTxt.text] && ![self.repwdTxt.text isEqualToString:(self.pwdTxt.text)]){
            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                            message:@"两次密码不一致"
                                                           delegate:nil
                                                  cancelButtonTitle:@"确定"
                                                  otherButtonTitles:nil];
            [alert show];
            return;
        }
        
    }
}


- (IBAction)next:(id)sender {
    
    if(self.pwdTxt.text == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请填写密码"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
        
    } else if(self.repwdTxt.text == nil){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                        message:@"请填写确认密码"
                                                       delegate:nil
                                              cancelButtonTitle:@"确定"
                                              otherButtonTitles:nil];
        [alert show];
        return;
        
    } else {
        if (self.networkManager == nil) {
            self.networkManager = [[NetworkManager alloc] init];
        }
        //_phone=@"13814313712";
        NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:
                               self.pwdTxt.text,@"pwd",_phone,@"phone" ,nil];
        [self.HUD show:YES];
        [self.networkManager requestWithMethod:resetPwd parameter:param delegate:self];
        
    }
}


- (void)network:(NetworkManager *)net methodId:(RequestMethodId)methodId didRequestFinishedWithResult:(id)data {
    [self.HUD hide:YES];
    //[self endRefresh];
    //self.isRefreshing = NO;
    
    NSLog(@"@@@@@@@@@@@@@@@@resetPwd");
    if (methodId == resetPwd) {
        if ([data isKindOfClass:[NSDictionary class]]) {
            NSString * result = [data objectForKeyOrNil:@"result"];
            NSDictionary * resultinfo = [data objectForKeyOrNil:@"resultinfo"];
            if([result isEqualToString:@"000"]){
                
                    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                    message:@"重置成功，请重新登录"
                                                                   delegate:nil
                                                          cancelButtonTitle:@"确定"
                                                          otherButtonTitles:nil];
                    [alert show];
                
                LoginViewController *problemVC = (LoginViewController *)[self.storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
                UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:problemVC];
                DDMenuController *menuVC = [[DDMenuController alloc] initWithRootViewController:navController];
//                [self presentViewController:menuVC animated:YES completion:^{}];
                [self.navigationController pushViewController:menuVC animated:YES];
            } else if([result isEqualToString:@"001"]){
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@""
                                                                message:@"找回密码失败"
                                                               delegate:nil
                                                      cancelButtonTitle:@"确定"
                                                      otherButtonTitles:nil];
                [alert show];
            }
            
            
        }
        
    }
}


- (BOOL) isBlankString:(NSString *)string {
    if (string == nil || string == NULL) {
        return YES;
    }
    if ([string isKindOfClass:[NSNull class]]) {
        return YES;
    }
    if ([[string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
